﻿using Furion.DatabaseAccessor;
using iemtm.WeChat.Core.Extension.Helper;
using iemtm.WeChat.Core.Model;
using iemtm.WeChat.Core.Util;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace iemtm.WeChat.Core.Quartz.Job
{
    [DisallowConcurrentExecution]//前一次任务未执行完成时不触发下次任务
    public class WxPushAlarm : IJob
    {
      
        public Task Execute(IJobExecutionContext context)
        {
            return Task.Run(() =>
            {
                #region 推送退服信息
                try
                {
                    if (DateTime.Now.Hour >= WeChatHelper.WeiXinConfig.pushStartTime && DateTime.Now.Hour <= WeChatHelper.WeiXinConfig.pushStopTime) //8点到22点才推送
                    {
                        if ((DateTime.Now.Minute == WeChatHelper.WeiXinConfig.pushMinute) && (DateTime.Now.Second >= 5 && DateTime.Now.Second < 10))//推送时间，整点推送
                        {
                            Log log = new Log() { LogName = "推送告警信息成功", LogDetails = "成功", CreateTime = DateTime.Now };
                            Db.GetRepository<Log>().Insert(log);

                            //读取微信配置信息
                          var userList=   Db.GetRepository<UserAccount>().Where(c => c.WxOpenId != null && c.WxOpenId != "").ToList(); //只获取已经绑定微信的帐号
                       
                            foreach (var item in userList)
                            {
                                if (item.RoleCode == "dwadminGZ" || item.RoleCode == "subadmin" || item.RoleCode == "gzdwsubadmin")
                                {
                                    System.Threading.Thread.Sleep(1000);
                                    Task.Run(() =>
                                    {
                                       
                                        AlarmCountModel model =Common.GetPushAlarmCount(item.UserCode);
                                        //执行推送方法
                                        WeiXinPush.SendAlarmTemplateMsg(item.UserCode, model);                                    

                                    });
                                }

                            }
                        }
                    }



                }
                catch (Exception e)
                {
                    Db.GetRepository<Log>().Insert(new Log() { LogName = "推送告警信息失败", LogDetails = e.ToString(), CreateTime = DateTime.Now });
                }
                #endregion
            });
        }



    }
}
